import grpc
import peewee
from time import time

from search_public_srv.proto import public_pb2, public_pb2_grpc
from google.protobuf.empty_pb2 import Empty
from search_public_srv.model.models import Banner, Notice
from search_public_srv.utils.grpc_utils import response_fail
from loguru import logger


def convert_notice_to_rsp(notice):
    rsp = public_pb2.NoticeInfoRsp()
    rsp.id = notice.id
    rsp.title = notice.title
    rsp.content = notice.content
    rsp.status = notice.status
    rsp.sort = notice.sort
    return rsp


def convert_banner_to_rsp(banner):
    rsp = public_pb2.BannerInfoRsp()
    rsp.id = banner.id
    rsp.title = banner.title
    rsp.imgUrl = banner.img_url
    rsp.status = banner.status
    rsp.sort = banner.sort
    rsp.url = banner.url
    rsp.about = banner.about
    return rsp


class PublicServicer(public_pb2_grpc.PublicServicer):
    @logger.catch
    def GetNoticeList(self, request: public_pb2.NoticeRequest, context):
        rsp = public_pb2.NoticeListRsp()
        notice = Notice().select().order_by(Notice.sort.asc())
        if request.title:
            notice = notice.where(Notice.title % f"%{request.title}%")
        if request.status:
            notice = notice.where(Notice.status == request.status)
        rsp.total = notice.count()
        page = 1
        limit = 10
        if request.page:
            page = request.page
        if request.limit:
            limit = request.limit
        offset = (page - 1) * limit
        notice = notice.limit(limit).offset(offset)
        for item in notice:
            rsp.data.append(convert_notice_to_rsp(item))

        return rsp

    @logger.catch
    def GetNoticeInfoById(self, request: public_pb2.NoticeRequest, context):
        try:
            notice = Notice().get(Notice.id == request.id)
            return convert_notice_to_rsp(notice)
        except peewee.DoesNotExist:
            response_fail(context, grpc.StatusCode.NOT_FOUND, "公告不存在")
            return public_pb2.NoticeInfoRsp()

    @logger.catch
    def CreateNotice(self, request: public_pb2.NoticeRequest, context):
        try:
            notice = Notice()
            notice.title = request.title
            notice.content = request.content
            notice.status = request.status
            notice.sort = request.sort
            notice.save()
            return convert_notice_to_rsp(notice)
        except peewee.PeeweeException as e:
            response_fail(context, grpc.StatusCode.INTERNAL, "服务器异常")
            return public_pb2.NoticeInfoRsp()

    @logger.catch
    def UpdateNotice(self, request: public_pb2.NoticeRequest, context):
        try:
            notice = Notice().get(Notice.id == request.id)
            notice.title = request.title
            notice.content = request.content
            notice.status = request.status
            notice.sort = request.sort
            notice.save()
            return Empty()
        except peewee.DoesNotExist:
            response_fail(context, grpc.StatusCode.NOT_FOUND, "公告不存在")
            return Empty()
        except peewee.PeeweeException as e:
            response_fail(context, grpc.StatusCode.INTERNAL, "服务器异常")
            return Empty()

    @logger.catch
    def DeleteNotice(self, request: public_pb2.NoticeRequest, context):
        try:
            notice = Notice().get(Notice.id == request.id)
            notice.soft_delete()
            return Empty()
        except peewee.DoesNotExist:
            response_fail(context, grpc.StatusCode.NOT_FOUND, "公告不存在")
            return Empty()

    @logger.catch
    def GetBannerList(self, request: public_pb2.BannerRequest, context):
        rsp = public_pb2.BannerListRsp()
        banner = Banner().select()

        if request.title:
            banner = banner.where(Banner.title % f"%{request.title}%")
        if request.status:
            banner = banner.where(Banner.status == request.status)
        rsp.total = banner.count()
        page = 1
        limit = 10
        if request.page:
            page = request.page
        if request.limit:
            limit = request.limit
        offset = (page - 1) * limit
        banner = banner.limit(limit).offset(offset)
        for item in banner:
            rsp.data.append(convert_banner_to_rsp(item))
        return rsp

    @logger.catch
    def GetBannerInfoById(self, request: public_pb2.BannerRequest, context):
        try:
            banner = Banner().get(Banner.id == request.id)
            return convert_banner_to_rsp(banner)
        except peewee.DoesNotExist:
            response_fail(context, grpc.StatusCode.NOT_FOUND, "轮播图不存在")
            return public_pb2.BannerInfoRsp()

    @logger.catch
    def CreateBanner(self, request: public_pb2.BannerRequest, context):
        try:
            banner = Banner()
            banner.title = request.title
            banner.img_url = request.imgUrl
            banner.status = request.status
            banner.sort = request.sort
            banner.url = request.url
            banner.about = request.about
            banner.save()
            return convert_banner_to_rsp(banner)

        except Exception as e:
            response_fail(context, grpc.StatusCode.INTERNAL, f"内部错误:{str(e)}")
            return public_pb2.BannerInfoRsp()

    @logger.catch
    def UpdateBanner(self, request: public_pb2.BannerRequest, context):
        try:
            banner = Banner().get(Banner.id == request.id)
            banner.title = request.title
            banner.img_url = request.imgUrl
            banner.status = request.status
            banner.sort = request.sort
            banner.url = request.url
            banner.about = request.about
            banner.save()
            return Empty()
        except peewee.DoesNotExist:
            response_fail(context, grpc.StatusCode.NOT_FOUND, "轮播图不存在")
            return Empty()
        except peewee.PeeweeException as e:
            response_fail(context, grpc.StatusCode.INTERNAL, "服务器异常")
            return Empty()

    @logger.catch
    def DeleteBanner(self, request: public_pb2.BannerRequest, context):
        try:
            banner = Banner().get(Banner.id == request.id)
            banner.soft_delete()
            return Empty()
        except peewee.DoesNotExist:
            response_fail(context, grpc.StatusCode.NOT_FOUND, "轮播图不存在")
            return Empty()
